package model.dao;

import model.entity.Book;
import org.junit.Test;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.List;
import java.util.Map;

public class SearchDaoImpl implements SearchDao {

    private JdbcTemplate template;

    public SearchDaoImpl(JdbcTemplate template) {
        this.template = template;
    }
    public SearchDaoImpl() {
        this.template = new JdbcTemplate(BaseDao.getDataSource());
    }


    @Override
    public Book searchByNo(String bookNo) {
        String sql = "select * from books where bookNo=?";
        Book book = template.queryForObject(sql, new String[]{bookNo}, new int[]{1}, new BeanPropertyRowMapper<Book>(Book.class));
        return book;
    }

    @Override
    public List<Book> searchByName(String bookName) {
        String sql = "select * from books where bookName=?";
        List<Book> books = template.query(sql, new String[]{bookName}, new BeanPropertyRowMapper<Book>(Book.class));
        return books;
    }

    @Test
    public void test() {
        List<Book> res=searchByName("001");
        System.out.println(res.isEmpty());
    }
}


